﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RTGS.Library.Report;
using System.Configuration;
using System.Data;
using Microsoft.Reporting.WebForms;
using RTGS.Library.DataAccess.Rtgs;
using RTGS.Library.Entities.Rtgs;
using RTGS.Library.DataAccess.UserManagement;
using RTGS.Library.Entities.UserManagement;


namespace RTGS.Web.page.report
{
    public partial class page_report_audit_trail_report : BasePage
    {

        ReportStpLog reportDal = new ReportStpLog(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        private stpLogRepo stpLogDal = new stpLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);

        private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        private sysTLog log = new sysTLog();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Log Type
            
                List<stpLog> listLogType = stpLogDal.GetList();
                var trn = from p in listLogType
                          orderby p.loglevel
                          group p by p.loglevel into g
                          select new { loglevel = g.Key, Accounts = g };

                // Output the email address and the count of records for each group
                foreach (var item in trn)
                {
                    ddlLogType.Items.Add(new ListItem(item.loglevel, item.loglevel));
                }

                ddlLogType.Items.Insert(0, new ListItem("---Pilih Data---", string.Empty));

            }
        }
    

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
       
            try
            {
                DataTable dt = reportDal.GetReportStpLog();

                rsViewer.Reset();
                rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/rptStpLog.rdlc");
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "dsStpLog";
                rds.Value = dt;

                //ReportParameter[] param = new ReportParameter[5];
                //param[0] = new ReportParameter("thn_ang", year);
                //param[1] = new ReportParameter("semester", semester);
                //param[2] = new ReportParameter("eselon", eselon);
                //param[3] = new ReportParameter("wilayah", wilayah);
                //param[4] = new ReportParameter("uakpb", satker);

                //rsViewer.LocalReport.SetParameters(param);


                rsViewer.LocalReport.DataSources.Clear();
                rsViewer.LocalReport.DataSources.Add(rds);
                rsViewer.LocalReport.Refresh();

                log = new sysTLog();
                log.logEvent = "View Report Audit Trail Success";
                log.logMessage = string.Empty;

            }
            catch (Exception ex)
            {
                log = new sysTLog();
                log.logEvent = "View Report Audit Trail Error";
                log.logMessage = ex.Message;

                Response.Write(ex.Message);
                
            }
            finally
            {
                log.logSource = "audit_trail_report.aspx";
                log.createdBy = base.UserLoggedIn;
                log.createdDate = DateTime.Now;
                logDal.Save(log);
            }
        }
    }
}